package com.xuecheng.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.comment.model.dto.CommentDTO;
import com.xuecheng.api.comment.model.vo.CommentVO;
import com.xuecheng.api.teaching.model.dto.CompanyDTO;
import com.xuecheng.api.teaching.model.qo.QueryCompanyModel;
import com.xuecheng.api.teaching.model.vo.CompanyVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.teaching.agent.CommentApiAgent;
import com.xuecheng.teaching.agent.CourseBaseApiAgent;
import com.xuecheng.teaching.common.constant.TeachingErrorCode;
import com.xuecheng.teaching.convert.CompanyConvert;
import com.xuecheng.teaching.entity.Company;
import com.xuecheng.teaching.mapper.CompanyMapper;
import com.xuecheng.teaching.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService{

    @Autowired
    private CommentApiAgent commentApiAgent;

    @Autowired
    private CourseBaseApiAgent courseBaseApiAgent;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public CompanyDTO getByTenantId(Long tenantId) {
        if (tenantId == null || tenantId <= 0) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getTenantId, tenantId);
        Company company = this.getOne(queryWrapper);
        return CompanyConvert.INSTANCE.entity2dto(company);
    }
    /*

     */
    @Override
    public CommentDTO createComment(CommentVO vo, Long companyId, String username) {
        //1.判断关键数据
        //     评论内容，用户名是否存在
        //     	不存在：抛出异常
        //1.1判断是否登录
        //1.2判断前端传入的数据是否为空
        //1.3用户评分0-5不能为空
        //1.3评论对象ID[课程发布ID]不能为空

        //数据库的必填验证  ---评论内容
        //根据getTargetId获取课程信息
        //判断课程是否存在
        //判断课程的状态
        //判断是否删除
        //判断是否发布
        // TODO:
        //2.2判断课程是否收费（非必须）
        //			收费：查询用户是否购买该课程，未购买不允许评论
        // TODO:
        //			2.2.1根据用户信息和课程id查询订单是否已付款
        //					未付款，不允许评论

        //新增评论

        return null;
    }

    @Override
    public RestResponse saveCompany(CompanyVO companyVO) {
        companyMapper.save(companyVO);
        Long companyId =companyVO.getCompanyId();
        if (companyId != null) {
            return RestResponse.success();
        } else {
            return RestResponse.validfail(TeachingErrorCode.E_130506);
        }
    }


    @Override
    public PageVO pageQuery(PageRequestParams params, QueryCompanyModel model) {
        if (params.getPageNo()<1){
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }
        if (params.getPageNo() < 1) {
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        Page<Company> basePage =new Page<>(params.getPageNo(),params.getPageSize());

        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(StringUtil.isNotBlank(model.getStatus()),Company::getStatus,model.getStatus());

        Page<Company> page = this.page(basePage, queryWrapper);

        List<Company> records=page.getRecords();
        long total = page.getTotal();

        List<CompanyDTO>dtos= Collections.emptyList();

        if (!(CollectionUtils.isEmpty(records))) {
            dtos = CompanyConvert.INSTANCE.entitys2dtos(records);
        }
        PageVO pageVO = new PageVO(dtos, total, params.getPageNo(), params.getPageSize());
        return pageVO;

    }



}
